/**
 * Project Name:九色鹿
 * File Name:CommonPage.java
 * Package Name:com.fxp.project.jiuselu.admin.domain
 * Date:2021-08-26 11:30 下午
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.web.domain;

import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * ClassName: CommonPage<br/>
 * Function: 公用的分页数据<br/>
 * Date: 2021-08-26 11:30 下午<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK 1.8
 */
@Data
public class CommonPage<T> {
    private Integer pageNum;
    private Integer pageSize;
    private Integer totalPage;
    private Long total;
    private List<T> list;

    /**
     * restPage: 将PageHelper分页后的list转为分页信息<br/>
     *
     * @param list
     * @param <T>
     * @return
     * @author Jason.Wang
     * @createTime 2021/3/21 19:22
     */
    public static <T> CommonPage<T> restPage(List<T> list) {
        CommonPage<T> result = new CommonPage<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * restPage: 将SpringData分页后的list转为分页信息<br/>
     *
     * @param pageInfo
     * @param <T>
     * @return
     * @author Jason.Wang
     * @createTime 2021/3/21 19:21
     */
    public static <T> CommonPage<T> restPage(Page<T> pageInfo) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage(pageInfo.getTotalPages());
        result.setPageNum(pageInfo.getNumber());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotalElements());
        result.setList(pageInfo.getContent());
        return result;
    }

    /**
     * restPage: 将PageInfo分页后的list转为分页信息<br/>
     *
     * @param pageInfo
     * @param <T>
     * @return
     * @author Jason.Wang
     * @createTime 2021/3/21 19:21
     */
    public static <T> CommonPage<T> restPage(PageInfo<T> pageInfo) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }
}
